﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using Website.Logger;
using DAL;
using BOL;
using WebLib;
using System.Web.Script.Services;
using System.Web.Services;
using UtilityExtensions;
namespace UILGestion.Admin
{
    public partial class TipoCambioReadForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                 // aqui de de ser por empresa  
                    internalid.Value = Request.Params["id"];
                    //idObra.Value = Request.Params["idObra"];
                    menuCtrl1.loadMenuAdmin();
                   FormatForm();

                }
                catch (SqlException sqlex)
                {
                    WebServerSideSqlError.Log(sqlex);

                    ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                     "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


                }
                catch (Exception ex)
                {
                    WebServerSideError.Log(ex);
                    ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                         "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

                }

            }
        }
        protected void AccionButton_Click(object sender, EventArgs e)
        {
         try  
            {

                if ((sender as Button).Text == "Restablecer")
                {
                        
                    FormatForm();
                }
                if ((sender as Button).Text == "Editar")
                {
                    Response.Redirect("TipoCambioEditForm.aspx?type=E&id="+internalid.Value,false );
                }
                if ((sender as Button).Text == "Nuevo")
                {
                    Response.Redirect("TipoCambioEditForm.aspx?type=A&id=" + internalid.Value, false);
                }
                   if ((sender as Button).ID == "eliminarButton")
                {
                    tipoCambioBL tpbol = new tipoCambioBL();

                      tpbol.Delete(int.Parse(internalid.Value));
                    
                    Response.Redirect("tipoCambioListForm.aspx", true);
                }
              
                if ((sender as Button).ID == "anteriorButton")
                {

                    dbGestionDataContext _db = 
                        new dbGestionDataContext(Parametros.strConnString);

                    DAL.TipoCambio tp = new DAL.TipoCambio();

                    int idAnterior = tp.getidAnterior(_db, int.Parse(internalid.Value));
                    Response.Redirect("TipoCambioReadForm.aspx?id=" + idAnterior, false);

                  
                }
                if ((sender as Button).ID == "siguienteButton")
                {

                    dbGestionDataContext _db =
                       new dbGestionDataContext(Parametros.strConnString);

                    DAL.TipoCambio tp = new DAL.TipoCambio();

                    int idSiguiente = tp.getidSiguiente(_db, int.Parse(internalid.Value));
                    Response.Redirect("TipoCambioReadForm.aspx?id=" + idSiguiente, false);
                }




            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);

                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                     "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }




        }
        private void ShowAllComboBox()
        {
            //dbGestionDataContext _db = new dbGestionDataContext(Parametros.strConnString);
            //var q = from i in _db.Moneda
            //        select i;


            //monedaBaseDropDownList.DataTextField = "descripcion";
            //monedaBaseDropDownList.DataValueField = "idInternal";

            //monedaBaseDropDownList.DataSource = q;
            //monedaBaseDropDownList.DataBind();


            //monedaCambioDropDownList.DataTextField = "descripcion";
            //monedaCambioDropDownList.DataValueField = "idInternal";

            //monedaCambioDropDownList.DataSource = q;
            //monedaCambioDropDownList.DataBind();





        }

      
        private void editObject()
        {
            

            DAL.TipoCambio tipoCambio= 
                DbUtil.Db.TipoCambios.Single(p=>p.idInternal==int.Parse(internalid.Value));
            
            fechaLabel.Text=tipoCambio.fecha.Value.ToShortDateString();
            tipoCambioLabel.Text=tipoCambio.monto.Value.ToString();
            monedaBaseLabel.Text=tipoCambio.Moneda.descripcion;
            monedaCambioLabel.Text = tipoCambio.Moneda1.descripcion;
            tipoCambioCompraTXT.Text=tipoCambio.montoCompra.ToString();
            Empleado empleado=DbUtil.Db.Empleados.SingleOrDefault(p=>p.idInternal==tipoCambio.usuarioMod.ToInt());
            usuarioModLBL.Text = empleado!=null?empleado.descripcion:"";
            FechaModLBL.Text = tipoCambio.fechaMod!=null?tipoCambio.fechaMod.Value.ToShortDateString():"";
          
        }

        private void FormatForm()
        {
            //--------------------
            int idEmpleadoEmisor = int.Parse(System.Web.HttpContext.Current.Session["idEmpleado"].ToString());
            bool acceso = new DAL.FormularioAcceso().VerificarAcceso(DbUtil.Db,
                idEmpleadoEmisor,
                12, //tipoCambio
                1); //Crear
            //------------------------- 
            // los paneles presentes en el formulario
            pnlMain.Visible = true;
            PanelBody.Visible = true;
            editObject();


        }

        protected void refreshPartida_Click(object sender, EventArgs e)
        {
            try
            {
                FormatForm();
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);

                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                     "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }
            

        }

       

        
       
    }
}